import axios from "axios";
import { Loading } from "element-ui";

const service = axios.create({
  baseURL: "http://rap2.taobao.org:38080/app/mock/260039"
});

let loadingInstance = null;

//请求拦截
service.interceptors.request.use(config => {
  //element-ui的一个加载插件加载效果
  loadingInstance = Loading.service({
    text: "获取数据中",
    spinner: "el-icon-loading",
    background: "rgba(0,0,0,.8)",
    target: ".el-table"
  });
  if (config.method === "post") loadingInstance.close();
  return config;
});
//响应拦截
service.interceptors.response.use(result => {
  loadingInstance.close();
  if (result.status === 200) {
    return result.data.data;
  }
  return Promise.reject(result);
});

export default service;
